//
//  DirectPlayViewController.m
//  LOLVideoPlayer
//
//  Created by SXW on 16/3/29.
//  Copyright © 2016年 盛宣伟. All rights reserved.
//

#import "DirectPlayViewController.h"


@interface DirectPlayViewController ()
@property (nonatomic, strong) MPMoviePlayerViewController *MVPlayer;
@end

@implementation DirectPlayViewController
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.vedioToUrl = [NSString stringWithFormat:@"http://www.demaxiya.com/app/index.php?m=play&vid=&quality=2&rand_2=2"];
    
    //    NSURL *url = [NSURL URLWithString:self.vedioToUrl];
    //    NSLog(@"%@",url);
    //
    //    //初始化属性的时候，尽量用下划线调用属性，不然涉及到内存问题
    //    _MVPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    //    _MVPlayer.view.frame = self.view.frame;
    //    [self.view addSubview:_MVPlayer.view];
    //    //播放视频
    //    [_MVPlayer play];
    
    [self playVideo:_vedioToUrl];
}

#pragma mark - 播放视频

- (void)playVideo:(NSString *)playUrl{
    self.MVPlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:self.vedioToUrl]];
    self.MVPlayer.moviePlayer.shouldAutoplay = YES;
    
    [self.MVPlayer.moviePlayer prepareToPlay];
    [self.MVPlayer.moviePlayer play];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playEnd:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self presentViewController:self.MVPlayer animated:YES completion:nil];
}

-(void)playEnd:(NSNotification *)notify{
    NSInteger reason = [[[notify userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
    switch (reason) {
        case 1:
            NSLog(@"正常结束");
            break;
        case 2:
            NSLog(@"用户退出");
            [self.navigationController popViewControllerAnimated:NO];
            break;
        default:
            NSLog(@"异常退出");
            break;
    }
    
}

@end
